package nl.haemers.testrunners 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import nl.haemers.base.BaseMovieClip;
	import nl.haemers.base.BaseSprite;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class BaseButtonRunner extends BaseMovieClip
	{
		public var scaled				:Boolean;
		
		public function BaseButtonRunner() 
		{
			super();
		}
		
		public function start() : void
		{
			configListeners();
			
			graphics.beginFill(0xFF0000);
			graphics.drawCircle(stage.stageWidth * .5, stage.stageHeight * .5, 20);
			graphics.endFill();
			
			addChild(createSprite(true));
		}
		
		protected function configListeners() : void
		{
			addEventListener(MouseEvent.CLICK, bRunnerClickHandler);
		}
		
		protected function bRunnerClickHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			if (scaled)
			{
				scale = 1;
				scaled = false;
			}
			else
			{
				scale = .2;
				scaled = true;
			}
		}
		
		protected function clickHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			addChild(createSprite());
		}
		
		protected function createSprite(creater:Boolean=false) : BaseSprite
		{
			var spr:BaseSprite = new BaseSprite();
			if (!creater)
			{
				spr.graphics.beginFill(0x000000);
			}
			else
			{
				spr.graphics.beginFill(0xFF00FF);
			}
			spr.graphics.drawRect(0, 0, 20, 20);
			spr.graphics.endFill();
			
			spr.x = int(Math.random() * (stage.stageWidth - 10));
			spr.y = int(Math.random() * (stage.stageHeight - 10));
			if (!creater)
			{
				spr.addEventListener(MouseEvent.CLICK, sprClickHandler);
			}
			else
			{
				spr.addEventListener(MouseEvent.CLICK, clickHandler);
			}
			
			return spr;
		}
		
		protected function sprClickHandler(e:MouseEvent) : void
		{
			e.stopImmediatePropagation();
			
			var spr:BaseSprite = e.target as BaseSprite;
			removeChild(spr);
			spr.destroy();			
		}
	}

}